package cur.nature.component.quartz.dao;

import cur.nature.component.quartz.config.CompQuartzDBConfig;
import cur.nature.component.quartz.model.Component_QuartzPO;
import cur.nature.component.quartz.model.Component_QuartzVO;
import cur.nature.framework.core.util.ObjectUtil;
import cur.nature.framework.core.util.StringUtil;
import cur.nature.framework.mvc.base.BaseVO;
import cur.nature.framework.mvc.crud.CrudDao;
import cur.nature.tinyorm.core.bean.PageBean;

import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;

/**
 * 任务调度 Dao
 * 作者：Auto Generator By wujian
 * 生成日期：2023-10-11 21:44:26
 */
@Component
@DependsOn(CompQuartzDBConfig.TEMPLATE)
@ConditionalOnProperty( value = {"cur.component.quartz.enable"})
public class Component_QuartzDao extends CrudDao<Component_QuartzPO, Component_QuartzVO> {
    public Component_QuartzDao(@Qualifier(CompQuartzDBConfig.TEMPLATE) DefaultJdbcTemplate template) {
        super(Component_QuartzPO.class, Component_QuartzVO.class,template);
    }

    public Component_QuartzVO getByCond(String jobGroup, String jobCode) throws Exception {
        final String sql = " and job_group_code ='"+ jobGroup +"' and job_code = '" +jobCode+ "'";
        List<BaseVO> list = this.queryByCond(sql);
        return ObjectUtil.isNotEmpty(list) ? (Component_QuartzVO) list.get(0) : null;
    }

    public static class CurAdminTemplate  extends DefaultJdbcTemplate {
        public CurAdminTemplate(DataSource dataSource) {
            super(dataSource);//设置数据连接池
        }
    }

    public List<Component_QuartzVO> list(Component_QuartzVO.list_Param param) throws Exception {
        StringBuilder condition = new StringBuilder(" 1=1 ");
        if(ObjectUtil.isNotEmpty(param.getIdList())){
            condition.append(" and a.id in("+ StringUtil.convertArrayToStr(param.getIdList()) +")");
        }
        String sql = "select a.cron_expression,a.id,a.job_class,a.job_data,a.job_group_code,a.job_code,a.job_name," +
                " a.remark,a.sched_code,a.state,from_unixtime(b.SCHED_TIME/1000, '%Y-%m-%d %H:%i:%S')  sched_time  " +
                " from component_quartz a  left join  QRTZ_FIRED_TRIGGERS b on " +
                " concat(a.job_group_code,'_trigger')=b.trigger_group and concat(a.job_code,'_trigger')=b.trigger_name " +
                " where "+condition;
        List<Component_QuartzVO> list = this.queryForListCust(sql,  new HashMap<>(), Component_QuartzVO.class);
        return list;
    }

    public PageBean<Component_QuartzVO> listPage(Component_QuartzVO.list_Param param) throws Exception {
        StringBuilder condition = new StringBuilder(" 1=1 ");
        if(ObjectUtil.isNotEmpty(param.getIdList())){
            condition.append(" and a.id in("+ StringUtil.convertArrayToStr(param.getIdList()) +")");
        }
        String sql = "select a.cron_expression,a.id,a.job_class,a.job_data,a.job_group_code,a.job_code,a.job_name," +
                " a.remark,a.sched_code,a.state,from_unixtime(b.SCHED_TIME/1000, '%Y-%m-%d %H:%i:%S')  sched_time,b.sched_name " +
                " from component_quartz a left join  QRTZ_FIRED_TRIGGERS b on " +
                " concat(a.job_group_code,'_trigger')=b.trigger_group and concat(a.job_code,'_trigger')=b.trigger_name " +
                " where "+condition;
        PageBean<Component_QuartzVO> list = queryForPage(sql, param.buildPage(),
                null, Component_QuartzVO.class);
        return list;
    }

    public List<Component_QuartzVO.QuartzVO> listQuartz(String jobName, String jobGroup) throws Exception {
        String sql = "select a.sched_name schedCode,a.job_group jobGroupCode,a.job_name jobCode,a.job_class_name jobClass,a.job_data,a.description,b.trigger_state,c.cron_expression " +
                "from QRTZ_JOB_DETAILS a,QRTZ_TRIGGERS b, QRTZ_CRON_TRIGGERS c " +
                "where a.job_group=b.job_group and a.job_name=b.job_name and b.trigger_group=c.trigger_group and b.trigger_name=c.trigger_name ";
        List<Component_QuartzVO.QuartzVO> list = this.queryForListCust(sql, Component_QuartzVO.QuartzVO.class);
        return list;
    }

}

